package com.haierp.task;

import com.google.common.collect.Lists;
import com.haierp.enums.GeneralStatus;
import com.haierp.model.InventoryListener;
import com.haierp.service.IInventoryListenerService;
import com.haierp.service.IOuterOrderService;
import com.haierp.service.xiaohongshu.IXiaohongshuService;
import com.haierp.service.yitao.IYtOrderService;
import com.haierp.service.youzan.IYouzanService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class AutoYiTaoTask {

    protected Logger logger = LogManager.getLogger(getClass());

    @Autowired
    private IYtOrderService iYtOrderService;
    @Autowired
    private SynInventoryTask synInventoryTask;

    @Autowired
    private IOuterOrderService outerOrderService;
    @Autowired
    private IInventoryListenerService inventoryListenerService;

    //每隔半小时执行一次
  //  @Scheduled(cron = "0 0/30 * * * ?")
    //@Scheduled(cron = "0/5 * * * * ? ")
    public void run() {
        logger.info("定时任务：自动去一桃下载订单===>Start");
        List<Long> outOrderIdList = iYtOrderService.yitaoTradesAdd();
        if (outOrderIdList.size() > 0) {
            //生成子订单并配货
            outOrderIdList.forEach(erpOrderId -> {
                outerOrderService.review(erpOrderId, "YT");
            });
        }
        logger.info("定时任务：自动去一桃下载订单===>End");
    }


    /**
     * 同步库存
     */
    //@Scheduled(cron = "0 0/5 * * * ?")
    @Scheduled(cron = "0 0/10 * * * ?")
    public void run1(){
        int companyId = 1;
        try {
            List<InventoryListener> invents = inventoryListenerService.getInventoryListener(companyId);////查询未同步信息
            //测试数据
            /*List<InventoryListener> invents = new ArrayList<>();
            InventoryListener inventoryListen = new InventoryListener();
            inventoryListen.setSkuId(31L);
            inventoryListen.setItemId(12L);
            invents.add(inventoryListen);*/
            if (CollectionUtils.isNotEmpty(invents)) {
                Set<Long> set = new HashSet<Long>();
                List<InventoryListener> updateListener = Lists.newArrayList();
                for (InventoryListener inventoryListener : invents) {
                    if (!set.contains(inventoryListener.getSkuId())) {
                        set.add(inventoryListener.getSkuId());
                        synInventoryTask.synSkuInventoryListenerYT(inventoryListener);
                    } else {
                        inventoryListener.setStatus(GeneralStatus.SUCCESS.getCode());
                        updateListener.add(inventoryListener);
                    }
                }
                if (!updateListener.isEmpty()) {
                    synInventoryTask.batchUpdateInventoryListener(updateListener);
                }
            }
        } catch (Exception e) {
            logger.info("AutoYouzanInventoryTask error:"+e);
        }
    }

}
